﻿Imports System.Xml
Imports System.IO

Public Class configurarInicial

#Region "Variáveis"

    Private _sCaminhoAplicacao As String

#End Region

#Region "Propriedades"

    Public Property dsModulos As New DataSet

#End Region

#Region "Construtor"

    Public Sub New(ByVal sCaminhoAplicacao As String)
        Me._sCaminhoAplicacao = sCaminhoAplicacao
    End Sub

#End Region

#Region "Métodos"

    Public Sub lerXML(ByVal sXML As String)

        Try

            If IO.File.Exists(Me._sCaminhoAplicacao & "\modulos.xml") Then
                Me.dsModulos.ReadXml(Me._sCaminhoAplicacao & "\modulos.xml")
            Else

                Dim doc As New XmlDocument
                doc.LoadXml(sXML)
                doc.Save(Me._sCaminhoAplicacao & "\modulos.xml")

                Me.dsModulos.ReadXml(Me._sCaminhoAplicacao & "\modulos.xml")

            End If

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Sub

    Public Sub atualizarXML(ByVal dsModulos As DataSet)

        Try
            dsModulos.WriteXml(Me._sCaminhoAplicacao & "\modulos.xml")
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Sub

    Public Function verificarCaminhoExistente(ByVal sCaminho As String) As Boolean

        Try

            If IO.File.Exists(sCaminho) Then
                Return True
            Else
                Return False
            End If

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Public Sub executarModulos(ByVal sCaminhoModulo As String,
                               ByVal sCaminhoInicial As String,
                               ByVal iCodUsuario As Integer,
                               ByVal iCodGrupoAcesso As Integer)

        Try

            Process.Start(sCaminhoModulo, sCaminhoInicial & " " & iCodUsuario & iCodGrupoAcesso)

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Sub

    Public Sub fecharModulos()

        Try

            Dim processo As New Process

            For Each proc In Process.GetProcesses
                Select Case proc.ProcessName
                    Case "Unify_Comercial"
                        proc.Kill()
                        proc.Close()
                    Case "Modulo2"
                        proc.Kill()
                        proc.Close()
                    Case "Modulo3"
                        proc.Kill()
                        proc.Close()
                    Case "Modulo4"
                        proc.Kill()
                        proc.Close()
                End Select
            Next

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Sub

#End Region

End Class
